<?php
namespace Tyrone\Command;
use Medoo\Medoo;
use Tyrone\Model\Game\LogLogin;
use Swoole\Coroutine;
use Tyrone\Model\Game\PayLog;
use Tyrone\Model\PlayerPay;

class RoleDayPayInfo extends Base
{

    public function divideHandle($mysql,$platform){
        $redis = $this->redis;
        $eDate = date("Ymd",strtotime($this->eDate));
        $tday = $day = date("Ymd",strtotime($this->sDate));
        $PlayerPayModel = new \Tyrone\Model\PlayerPay($mysql);
        $playerOnlineModel = new \Tyrone\Model\PlayerOnline($mysql);

        $where = [
            'p.platform' =>$platform,
        ];
        $where2 = [
            'platform' =>$platform,
        ];
        $datas = [];
        echo 'divideHandle-----Tyrone\Command\RolePayStatistics:execute_'.$platform.PHP_EOL;
        while($day <= $eDate) {
            //统计登录数据

            $data = $playerOnlineModel->getLoginPlayerPay($day,$where);
            echo 'divideHandle-----Tyrone\Command\RolePayStatistics:execute1_'.$platform.'_'.$day.PHP_EOL;
            //昨天的记录
            $beforeDay = date("Ymd", strtotime($day) - 86400);
            $beforeList = $playerOnlineModel->getDayPlayerData($beforeDay,$where2);
            echo 'divideHandle-----Tyrone\Command\RolePayStatistics:execute2_'.$platform.'_'.$day.PHP_EOL;
            //获取当天充值的玩家
            $payPlayerArr = $PlayerPayModel->getRoleGroupData($day,$where2);
            echo 'divideHandle-----Tyrone\Command\RolePayStatistics:execute3_'.$platform.'_'.$day.PHP_EOL;
            //获取历史
            $historyPlayerArr = $PlayerPayModel->getRoleGroupData(null,$where2 + [
                    'pay_date[<]' => $day,
                ]);
            echo 'divideHandle-----Tyrone\Command\RolePayStatistics:execute4_'.$platform.'_'.$day.PHP_EOL;
            foreach($data as $vo){

                $save = $vo;unset($save['id']);
                $save['pay_rmb'] = $vo["rmb"] ?: 0;unset($save["rmb"]);

                if(isset($payPlayerArr[$vo['player_id']])){
                    $save["is_pay"] = 1;
                    if(!isset($beforeList[$vo['player_id']]['contin_pay'])){
                        $save["contin_pay"] = 1;
                    }else{
                        $save["contin_pay"] = $beforeList[$vo['player_id']]['contin_pay'] + 1;
                    }
                }else{
                    $save["contin_pay"] = 0;
                    $save["is_pay"] = isset($historyPlayerArr[$vo['player_id']]) ? 2 : 0;
                }
                $save["contin_day"] = isset($beforeList[$vo['player_id']]) ? $beforeList[$vo['player_id']]['contin_day'] + 1 : 1;
                $datas[] = $save;
            }
            echo 'divideHandle-----Tyrone\Command\RolePayStatistics:execute5_'.$platform.'_'.$day.PHP_EOL;
            $day = date('Ymd',strtotime($day) + 86400);
        }
        foreach (array_chunk($datas,1000) as $items){
            $playerOnlineModel->updateData($items);
            usleep(100);
            echo 'divideHandle-----Tyrone\Command\RolePayStatistics:execute_updateData_'.$platform.'_'.$day.PHP_EOL;
        }
        echo 'divideHandle-----Tyrone\Command\RolePayStatistics:execute6_'.$platform.'_'.$day.PHP_EOL;
        return count($datas);
    }


    public  function execute()
    {
        return $this->divideCenter();
    }
}